#!/usr/bin/env python
#
#       persist.py
#
#       Copyright 2009 Chris Glass <tribaal@gmail.com>
#
#       This program is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime
from properties import props

class database_accessor():
    '''This is the main database access object. It holds all the CRUD
    (Create Read Update Delete) accessors, to hide SQL from the rest of
    the program.
    Plugin files should extend this interface.'''

    def __init__(self, config):
        pass

    def create_database(self):
        '''This creates the database tables if they don't already exist.'''
        pass

    def insert_cache(self, url, mime_type , date, data, headers=None):
        '''Inserts a cache entry in the database'''
        pass

    def retrieve_entry(self, url):
        '''Returns a row of data from the database as a dictionary'''
        pass

    def print_table(self) :
        '''Displays the content of the database (this is only for tests
        and/or debugging purposes)'''
        pass
        
    def get_insert_date_for_url(self, url):
        pass
    
    def delete_entry(self, url):
        ''' Deletes an entry an relevant headers '''
        pass

    def cleanup_cache(self, time):
        ''' This method cleans up all entries older than a given time, in seconds'''
        pass

    def _compute_page_number(self):
        '''We want to know the number of pages we can use, using the
        default pagezise and the specified max disk size.'''
        
    def _parse_headers(self, db_headers):
        '''Make the returned row (list) from the database in a nice 
        dictionnary.'''
        headers = {}
        for header in db_headers:
            headers[header[0]] = header[1]
        return headers
        

    def _datetime_from_iso_format(self, iso_string):
        '''While the syntax looks ugly, it's quite fast.
        In Python, splices (using the [:] operator) are much more
        efficient than standard String manipulation function like split()
        and join()'''
        year = int(iso_string[:4])
        iso_string = iso_string[5:] #Includes the separator
        month = int(iso_string[:2])
        iso_string = iso_string[3:]
        day = int(iso_string[:2])
        iso_string = iso_string[3:]
        hour = int(iso_string[:2])
        iso_string = iso_string[3:]
        minute = int(iso_string[:2])
        iso_string = iso_string[3:]
        second = int(iso_string[:2])
        iso_string = iso_string[3:]
        miliseconds = int(iso_string)
        date = datetime.datetime(year, month, day, hour, minute, second, miliseconds)
        return date


def load_plugin(plugin, config):
    """plugin is assumed to be theplugin's name string i.e. 'sqlite'"""
    mod = __import__(plugin)
    components = plugin.split('.')
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mod.database_accessor_implementation(config)






